package com.webwork.lims.transport.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.lims.common.dao.impl.SimpleDao;
import com.webwork.lims.transport.dao.CarsDao;
import com.webwork.lims.transport.dataobject.Car;
import com.webwork.lims.transport.dataobject.CarStatus;
import com.webwork.lims.transport.dataobject.TransType;
import com.webwork.lims.util.ColumnLimit;
import com.webwork.lims.util.DBSimpleSQL;
import com.webwork.lims.util.StrManager;

@Repository
public class CarsDaoImpl extends SimpleDao implements CarsDao {

	public CarsDaoImpl() {
		super();
		this.dbTable = "t_cars";
		dbColumns.clear();
		dbColumns.put("carNum", "CAR_NUM");
		dbColumns.put("zone", "ZONE");
		dbColumns.put("type", "TYPE");
		dbColumns.put("status", "STATUS");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof Car))
			return new ColumnLimit();
		Car o = (Car) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getCarNum())) {
			ret.add(dbColumns.get("carNum") + " = ");
		}
		if (o.getZoneCode() != 0) {
			ret.add(dbColumns.get("zone") + " = ");
		}
		if (o.getType() != TransType.NULL) {
			ret.add(dbColumns.get("type") + " = ");
		}
		if (o.getStatus() != CarStatus.NOSET) {
			ret.add(dbColumns.get("status") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		Car o = (Car) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getCarNum())) {
			ret.add(o.getCarNum());
		}
		if (o.getZoneCode() != 0) {
			ret.add(o.getZoneCode());
		}
		if (o.getType() != TransType.NULL) {
			ret.add(o.getType().name());
		}
		if (o.getStatus() != CarStatus.NOSET) {
			ret.add(o.getStatus().getCode());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		Car o = (Car) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getCarNum());
		objs.add(o.getZoneCode());
		objs.add(o.getType().name());
		objs.add(o.getStatus().getCode());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		Car ret = new Car();
		ret.setCarNum((String) map.get(dbColumns.get("carNum")));
		ret.setZoneCode((Integer) map.get(dbColumns.get("zone")));
		ret.setType(TransType.valueOf((String) map.get(dbColumns.get("type"))));
		ret.setStatus((Integer) map.get(dbColumns.get("status")));
		return ret;
	}
	
	@Override
	public void insert(Car o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(Car o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("carNum") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getCarNum());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(String carNum) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("carNum") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, carNum);
	}

	@Override
	public List<Car> query(Car o, int page, int size) {
		List<Car> ret = new ArrayList<Car>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((Car) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(Car o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
